/**
 * Created by XianTao on 2016/7/10
 * <p/>
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package org.lego.jpa.base.service;

import org.lego.jpa.base.entity.EmptyEntity;
import org.lego.jpa.base.repository.BaseRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import java.util.List;

public abstract class BaseService<T extends EmptyEntity, ID extends String> {
    protected abstract BaseRepository<T, String> getRepository();

    public List<T> findAll() {
        return getRepository().findAll();
    }

    public List<T> findAll(Sort sort) {
        return getRepository().findAll(sort);
    }

    public List<T> findAll(Iterable<String> ids) {
        return getRepository().findAll(ids);
    }

    public List<T> findAll(Specification<T> specification) {
        return getRepository().findAll(specification);
    }

    public List<T> findAll(Specification<T> specification, Sort sort) {
        return getRepository().findAll(specification, sort);
    }

    public Page<T> findAll(Pageable pageable) {
        return getRepository().findAll(pageable);
    }

    public Page<T> findAll(Specification<T> specification, Pageable pageable) {
        return getRepository().findAll(specification, pageable);
    }

    public List<T> save(Iterable<T> iterable) {
        return getRepository().save(iterable);
    }

    public T saveAndFlush(T entity) {
        return getRepository().saveAndFlush(entity);
    }

    public T findOne(ID id) {
        return getRepository().findOne(id);
    }

    public T findOne(Specification<T> specification) {
        return getRepository().findOne(specification);
    }

    public void flush() {
        getRepository().flush();
    }

    public void delete(ID id) {
        getRepository().delete(id);
    }

    public void delete(T entity) {
        getRepository().delete(entity);
    }

    public void deleteInBatch(Iterable<T> iterable) {
        getRepository().deleteInBatch(iterable);
    }

    public long count(Specification<T> specification) {
        return getRepository().count(specification);
    }

    public boolean exists(String id) {
        return getRepository().exists(id);
    }
}
